//
//  LRVisitorView.swift
//  LRWeibo
//
//  Created by admin on 15/9/21.
//  Copyright © 2015年 懒人. All rights reserved.
//

import UIKit
import SnapKit

class LRVisitorView: UIView {

// TODO: 添加协议
    
//    static let sharedTool = LRVisitorView() // 单利
    
     // MARK: - 根据控制器更改显示
    func setupInfo(imageName: String?, message: String)
    {
        messageLabel.text = message
        if imageName != nil
        {
            smalliconView.image = UIImage(named: imageName!)
            houseView.hidden = true
            sendSubviewToBack(maskIconView)
        }
        else
        {
            starAnim()
        }

    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 初始化页面
        setSubViews()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setSubViews()
    }
    
     // MARK: - 添加动画
    private func starAnim()
    {
        let cab = CABasicAnimation(keyPath: "transform.rotation")
        cab.toValue = 2 * M_PI
        cab.repeatCount = MAXFLOAT
        cab.duration = 15
        
        cab.removedOnCompletion = false
        
        // 添加
        smalliconView.layer.addAnimation(cab, forKey: nil)
        
    }
    
     // MARK: - 添加控件
    private func setSubViews()
    {
        addSubview(smalliconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        // 设置自动布局取消
        for v in subviews
        {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 添加约束
        // 动画
//        addConstraint(NSLayoutConstraint(item: smalliconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: smalliconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        // 采用SnapKip布局
        smalliconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        // 房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: smalliconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: smalliconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: smalliconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: smalliconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 遮盖图 VFL 
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[loginButton]", options: [], metrics: ["btnHeight": -70], views: ["mask": maskIconView, "loginButton": loginButton]))
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerButton.snp_top)
        }
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    

    
     // MARK: - 控件懒加载
    // 动画
    private lazy var smalliconView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮盖
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    // 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", fontSize: 14, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    // 登录
    lazy var loginButton: UIButton = UIButton(title: "登录", fontSize: 14, color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")

}
